package com.itaming.lycheeframework.core.config;

import com.itaming.lycheeframework.core.filter.XssFilter;
import com.itaming.lycheeframework.core.properties.LycheeXssProperties;
import jakarta.servlet.DispatcherType;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * XSS配置类
 *
 * @author A.Ming
 */
@AutoConfiguration
@EnableConfigurationProperties({LycheeXssProperties.class})
@RequiredArgsConstructor
public class XssConfiguration {

    /**
     * XSS配置参数
     */
    private final LycheeXssProperties lycheeXssProperties;

    /**
     * XSS过滤器
     */
    @Bean
    public FilterRegistrationBean<XssFilter> lycheeXssFilter() {
        FilterRegistrationBean<XssFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setDispatcherTypes(DispatcherType.REQUEST);
        registrationBean.setFilter(new XssFilter(lycheeXssProperties));
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("xssFilter");
        return registrationBean;
    }

}
